package com.testcolision.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.testcolision.OnscreenControlListener;
import com.testcolision.OnscreenControlRenderer;
import com.testcolision.Renderer;
import com.testcolision.deprecated.Simulation;

public class GameScreen extends ExtendedScreen {

	private final Simulation simulation;
	private final Renderer renderer;
	private OnscreenControlListener controlListener;
	private OnscreenControlRenderer controlRenderer;
	private boolean leftHeld;
	private boolean rightHeld;
	private boolean upHeld;
	private boolean downHeld;

	public GameScreen() {
		simulation = new Simulation();
		renderer = new Renderer();
		controlListener = new OnscreenControlListener();
		controlRenderer = new OnscreenControlRenderer();
	}

	@Override
	public void update(float delta) {

		upHeld = Gdx.input.isKeyPressed(Keys.DPAD_UP) || Gdx.input.isKeyPressed(Keys.W);

		downHeld = Gdx.input.isKeyPressed(Keys.DPAD_DOWN) || Gdx.input.isKeyPressed(Keys.S);

		leftHeld = Gdx.input.isKeyPressed(Keys.DPAD_LEFT) || Gdx.input.isKeyPressed(Keys.A);

		rightHeld = Gdx.input.isKeyPressed(Keys.DPAD_RIGHT) || Gdx.input.isKeyPressed(Keys.D);

		if (leftHeld)
			simulation.steering = -1;
		else if (rightHeld)
			simulation.steering = 1;
		else
			simulation.steering = 0;

		if (upHeld)
			simulation.throttle = 1;
		else
			simulation.throttle = 0;

		if (downHeld)
			simulation.brakes = 1;
		else
			simulation.brakes = 0;

		simulation.update(delta);

		// Input listener multitouch
		// for (int i = 0; i < 2; i++) {
		// if (Gdx.input.isTouched(i)) {
		//
		// if (controlListener.leftButton(Gdx.input.getX(i), Gdx.input.getY(i)))
		// {
		// simulation.steerLeft(delta, 0.5f);
		// } else if (controlListener.rightButton(Gdx.input.getX(i),
		// Gdx.input.getY(i))) {
		// simulation.steerRight(delta, 0.5f);
		// } else if (controlListener.upButton(Gdx.input.getX(i),
		// Gdx.input.getY(i))) {
		// simulation.accelerate(delta, -1);
		// } else if (controlListener.controlButton(Gdx.input.getX(i),
		// Gdx.input.getY(i))) {
		// simulation.accelerate(delta, 1);
		// }
		// else{
		// simulation.rotateCamera(Gdx.input.getX(i), Gdx.input.getY(i));
		// }
		// }
		// }

	}

	@Override
	public void draw(float delta) {
		renderer.render(simulation, delta);
		controlRenderer.render();
	}

	@Override
	public boolean isDone() {
		return false;
	}

}
